import { sendValidCode, listByKind } from '../../api/base'
import { saveBaseInfo, userGetPhoneNumber } from '../../api/user'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageHeight: 0,
    form: {
      avatarUrl: '',
      email: '',
      identityType: '',
      mobileNumber: '',
      nickname: '',
      sex: '',
      verifyCode: '',
      educational: ''
    },

    educationList: [],
    showEducation: false,
    selEducation: '',

    isAgree: false,
    avatarUrl: '',
    sex: '',
    identity: '',
    isCountDown: false,
    time: 60 * 1000,
    isDisabled: false
  },

  onLoad: function (options) {
    this.setPageSize();
    this.getEducational();
    this.setData({
      'form.nickname': wx.getStorageSync('userNickName'),
      'form.avatarUrl': wx.getStorageSync('userAvatar'),
      avatarUrl: wx.getStorageSync('userAvatar')
    })
  },

  back() {
    wx.navigateBack({ delta: 1 })
  },

  setPageSize() {
    let windowHeight
    wx.getSystemInfo({
      success: function(res) {
        windowHeight = res.windowHeight
      }
    });
    let query = wx.createSelectorQuery().in(this);
    query.select('.info-nav-wrap').boundingClientRect();
    query.exec((res) => {
      this.setData({
        pageHeight: windowHeight - res[0].height
      })
    });
  },

  navigationTo({ currentTarget = {} }) {
    wx.navigateTo({
      url: currentTarget.dataset.url + '?key=' + this.data.form[currentTarget.dataset.name]
    })
  },

  getEducational() {
    listByKind('educational').then(res => {
      this.setData({
        educationList: res
      })
    })
  },

  changeNickName({ detail = {} }) {
    this.setData({
      'form.nickname': detail.value
    })
  },

  changeEmail({ detail = {} }) {
    this.setData({
      'form.email': detail.value
    })
  },

  changePhone({ detail = {} }) {
    this.setData({
      'form.mobileNumber': detail.value
    })
  },

  changeVerifyCode({ detail = {} }) {
    this.setData({
      'form.verifyCode': detail.value
    })
  },

  getPhoneNumber(e) {
    const _this = this
    wx.checkSession({
      success() {
        console.log('chenggong')
        _this.setPhoneNumber(e.detail)
      },
      fail() {
        console.log('shibai')
        wx.login({
          success(res) {
            _this.setPhoneNumber(e.detail)
          }
        })
      }
    })
  },

  setPhoneNumber(detail) {
    userGetPhoneNumber({
      encryptedData: detail.encryptedData,
      iv: detail.iv
    }).then(res => {
      console.log(res)
      this.setData({
        'form.mobileNumber': res
      })
    })
  },

  getValidCode() {
    sendValidCode({
      phone: this.data.form.mobileNumber,
      bizTypeEnums: '完善资料'
    }).then(res => {
      wx.showToast({
        title: '短信已发送'
      })
      this.countDownStart()
    })
  },

  countDownStart() {
    this.setData({
      isCountDown: true
    })
    const countDown = this.selectComponent('.control-count-down');
    countDown.start();
  },

  countDownReset() {
    // const countDown = this.selectComponent('.control-count-down');
    // countDown.reset();
    this.setData({
      isCountDown: false
    })
  },

  /** 学历 start **/

  selectEducation() {
    this.setData({ showEducation: true })
  },
  cancelSelectEducation() {
    this.setData({ showEducation: false })
  },
  confirmSelectEducation(e) {
    this.setData({
      selEducation: e.detail.name,
      'form.educational': e.detail.code,
      showEducation: false
    })
  },
  /** 学历 end **/

  changeAgree(e) {
    this.setData({
      isAgree: !this.data.isAgree
    })
  },

  next() {
    function setToast(tips) {
      wx.showToast({
        title: tips,
        icon: 'none',
        duration: 2000
      })
    }
    if (!this.data.form.avatarUrl) {
      setToast('请选择头像');
      return false;
    } else if (!this.data.form.nickname) {
      setToast('请填写昵称');
      return false;
    } else if (!this.data.form.sex) {
      setToast('请选择性别');
      return false;
    } else if (!this.data.form.mobileNumber) {
      setToast('请输入手机号码');
      return false;
    } else if (!this.data.form.verifyCode) {
      setToast('请输入验证码');
      return false;
    } else if (!this.data.form.identityType) {
      setToast('请选择身份');
      return false;
    } else if (!this.data.form.educational) {
      setToast('请选择学历');
      return false;
    } else if (!this.data.isAgree) {
      setToast('请勾选同意《用户协议》');
      return false;
    }
    saveBaseInfo(this.data.form).then(res => {
      wx.navigateTo({
        url: this.data.form.identityType === 'STUDENT' ? '/pages/completeInfoStudent/completeInfoStudent' : '/pages/completeInfoTeacher/completeInfoTeacher'
      })
    })
  }
})
